package com.xxl.job.executor.service.jobhandler;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.common.config.EmailConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.vo.EmailVo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class LoanPendingMonitorJob {

    @Autowired
    private ILoanOrderService loanOrderService;

    @Autowired
    private AsyncService asyncService;

    @XxlJob("loanPendingMonitorJobHandler")
    public void execute() {
        XxlJobHelper.log("LoanPendingMonitorJob Start >>>>>>>>>>");

        QueryWrapper<LoanOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loan_status", 3).or().eq("loan_status", 1).select("pay_trans_serial_no", "loan_status");
        List<LoanOrder> list = loanOrderService.list(queryWrapper);

        String payTransSerialNo_1 = "";
        String payTransSerialNo_3 = "";
        for (int i = 0; i < list.size(); i++) {
            if (StringUtils.isNotBlank(list.get(i).getPayTransSerialNo())) {
                if (list.get(i).getLoanStatus() == 1) {
                    payTransSerialNo_1 += list.get(i).getPayTransSerialNo() + "<br/>";
                } else if (list.get(i).getLoanStatus() == 3) {
                    payTransSerialNo_3 += list.get(i).getPayTransSerialNo() + "<br/>";
                }
            }
        }

        //发送飞书
        XxlJobHelper.log("payTransSerialNo_1:" + payTransSerialNo_1);
        if (StringUtils.isNotBlank(payTransSerialNo_1) && payTransSerialNo_1.split("<br/>").length > 5) {
            XxlJobHelper.log("发送飞书");

            payTransSerialNo_1 = payTransSerialNo_1.substring(0, payTransSerialNo_1.length() - 1);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("payTransSerialNos", payTransSerialNo_1);
            asyncService.sendFeishu("monitor-group", "Loan_Pass_Monitor", jsonObject);
        }

        //推送邮件
        XxlJobHelper.log("payTransSerialNo_3:" + payTransSerialNo_3);
        if (StringUtils.isNotBlank(payTransSerialNo_3) && payTransSerialNo_3.split("<br/>").length > 5) {
            XxlJobHelper.log("发送邮件");

            payTransSerialNo_3 = payTransSerialNo_3.substring(0, payTransSerialNo_3.length() - 1);

            String content = "<html><head></head><body>Dear Paystack,<br/><br/>The following transactions from our disbursements status has been pending for a long time, which has seriously affected our business, please deal with it as soon as possible.<br/><br/>%s<br/><br/>Regards</body></html>";
            String format = String.format(content, payTransSerialNo_3);
            EmailVo emailVo = EmailVo.builder()
                    .subject("Payment transactions continue to be pending (Link Credit & Paystack)")
                    .cc(EmailConfig.getLoanCc().split(","))
                    .addressees(EmailConfig.getLoanAddress().split(","))
                    .content(format)
                    .build();
            asyncService.sendCustomizeEmail(emailVo);
        }

        XxlJobHelper.log("LoanPendingMonitorJob End >>>>>>>>>>");
    }
}
